package com.randi.system.service.impl.sysDict;

import com.github.pagehelper.PageInfo;
import com.randi.common.constant.CacheConstants;
import com.randi.common.utils.SecurityUtils;
import com.randi.system.domain.entity.sysDict.SysDict;
import com.randi.system.domain.params.sysDict.SysDictParam;
import com.randi.system.mapper.sysDict.SysDictMapper;
import com.randi.system.service.sysDict.ISysDictService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author ziye
 */
@Service
@CacheConfig(cacheNames = CacheConstants.SYS_DICT)
public class SysDictServiceImpl implements ISysDictService {

    @Resource
    private SysDictMapper sysDictMapper;

    /**
     * 分页查询业务列表
     *
     * @param sysDictParam sysDictType
     * @return 业务集合
     */
    @Override
    @Cacheable(keyGenerator = "baseKeyGenerator", unless = "#result == null")
    public PageInfo<SysDict> selectPageList(SysDictParam sysDictParam) {
        return new PageInfo<>(sysDictMapper.selectSysDictList(sysDictParam));
    }

    /**
     * 查看详情
     *
     * @return 业务集合
     */
    @Override
    @Cacheable(key = "#dictId", unless = "#result == null")
    public SysDict selectSysDictById(Long dictId) {
        return sysDictMapper.selectSysDictById(dictId);
    }

    /**
     * 插入数据
     *
     * @param sysDict sysDictType
     * @return 结果
     */
    @Override
    @CacheEvict(allEntries = true, beforeInvocation = true)
    public int insertSysDict(SysDict sysDict) {
        SecurityUtils.setInsertInfo(sysDict);
        return sysDictMapper.insert(sysDict);
    }

    /**
     * 更新数据
     *
     * @param sysDict SysDict
     * @return 结果
     */
    @Override
    @CacheEvict(allEntries = true, beforeInvocation = true)
    public int updateSysDict(SysDict sysDict) {
        SecurityUtils.setUpdateInfo(sysDict);
        return sysDictMapper.updateById(sysDict);
    }

    /**
     * 批量删除数据
     *
     * @param dictIds dictIds
     * @return status
     */
    @Override
    @CacheEvict(allEntries = true, beforeInvocation = true)
    public int deleteBatchSysDict(List<Long> dictIds) {
        return sysDictMapper.batchUpdateDeleted(dictIds);
    }

}
